<?php

namespace app\admin\controller\collage;

use app\admin\model\GroupMaster;
use app\admin\model\User;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="collage")
 */
class Collage extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Collage();

    }

    public function memberIndex($id)
    {
        $master_id = $id;
        return view('collage\collage\bind_member', compact('master_id'));
    }

    public function collageMember($id)
    {
        $master_id = $id;
        return view('collage\collage\collage_member', compact('master_id'));
    }

    public function memberList($master_id)
    {
        if (!is_numeric($master_id)) {
            $data = [
                'code' => 0,
                'msg' => '无数据',
                'data' => [],
            ];
            return json($data);
        }
        $groupMasterModel = new GroupMaster();
        $masterInfo = $groupMasterModel->find($master_id);
        if ($masterInfo->audit_status == 0)
        {
            $data = [
                'code' => 0,
                'msg' => '该团长处于待审核状态,审核通过绑定数据',
                'data' => [],
            ];
            return json($data);
        }
        if ($masterInfo->audit_status == 2)
        {
            $data = [
                'code' => 0,
                'msg' => '该团长处于审核不通过状态',
                'data' => [],
            ];
            return json($data);
        }
        if (!$masterInfo) {
            $data = [
                'code' => 0,
                'msg' => '无数据',
                'data' => [],
            ];
            return json($data);
        }
        $verification_id = $masterInfo->verification_id;
        if (empty($verification_id)) {
            $collageInfo = $this->model->where('master_id', $master_id)->find();
            if (!$collageInfo) {
                $data = [
                    'code' => 0,
                    'msg' => '无数据',
                    'data' => [],
                ];
                return json($data);
            }
            $user_id = explode(',', $collageInfo->user_id);
        } else {
            $verification_id = explode(',', $verification_id);
            $collageInfo = $this->model->where('master_id', $master_id)->find();
            if (!$collageInfo) {
                $data = [
                    'code' => 0,
                    'msg' => '无数据',
                    'data' => [],
                ];
                return json($data);
            }
            $user_id = explode(',', $collageInfo->user_id);
            $user_id = array_diff($verification_id, $user_id);
        }
        $userModel = new User();
        list($page, $limit, $where) = $this->buildTableParames();
        $count = $userModel
            ->whereIn('id',$user_id)
            ->where($where)
            ->count();
        $list = $userModel
            ->whereIn('id',$user_id)
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list,
        ];
        return json($data);
    }

    public function collageMemberList($master_id)
    {
        if (!is_numeric($master_id)) {
            $data = [
                'code' => 0,
                'msg' => '无数据',
                'data' => [],
            ];
            return json($data);
        }
        $groupMasterModel = new GroupMaster();
        $masterInfo = $groupMasterModel->find($master_id);
        if ($masterInfo->audit_status == 0)
        {
            $data = [
                'code' => 0,
                'msg' => '该团长处于待审核状态,审核通过绑定数据',
                'data' => [],
            ];
            return json($data);
        }
        if ($masterInfo->audit_status == 2)
        {
            $data = [
                'code' => 0,
                'msg' => '该团长处于审核不通过状态',
                'data' => [],
            ];
            return json($data);
        }
        if (!$masterInfo) {
            $data = [
                'code' => 0,
                'msg' => '无数据',
                'data' => [],
            ];
            return json($data);
        }
        $verification_id = $masterInfo->verification_id;
        if (!empty($verification_id)) {
            $collageInfo = $this->model->where('master_id', $master_id)->find();
            if (!$collageInfo) {
                $data = [
                    'code' => 0,
                    'msg' => '无数据',
                    'data' => [],
                ];
                return json($data);
            }
            $user_id = explode(',', $collageInfo->user_id);
        } else {
            $verification_id = explode(',', $verification_id);
            $collageInfo = $this->model->where('master_id', $master_id)->find();
            if (!$collageInfo) {
                $data = [
                    'code' => 0,
                    'msg' => '无数据',
                    'data' => [],
                ];
                return json($data);
            }
            $user_id = explode(',', $collageInfo->user_id);
            $user_id = array_diff($verification_id, $user_id);
        }
        $userModel = new User();
        list($page, $limit, $where) = $this->buildTableParames();
        $count = $userModel
            ->whereIn('id',$user_id)
            ->where($where)
            ->count();
        $list = $userModel
            ->whereIn('id',$user_id)
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list,
        ];
        return json($data);
    }

    public function bindMember()
    {
        $params = input();
        $params['user_id'] = implode(',', $params['user_id']);
        $res = \app\admin\model\GroupMaster::where('id', $params['master_id'])->update(['verification_id' => $params['user_id']]);
        if (!$res) {
            return json(['code' => 0, 'msg' => '添加核销员失败', 'data' => []]);
        }
        return json(['code' => 1, 'msg' => '添加成功', 'data' => []]);
    }
}